import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kepu/page/animal_list_page/animal_list_page.dart';
import 'package:kepu/page/home_page/widget/float_button.dart';
import 'package:kepu/page/plant_list_page/plant_list_page.dart';
import 'package:kepu/page/recommend_page/recommend_page.dart';
import '../../provider/theme_provider.dart';

class HomePage extends ConsumerStatefulWidget {
  const HomePage({super.key});

  @override
  ConsumerState<HomePage> createState() => _HomePageState();
}

class _HomePageState extends ConsumerState<HomePage>
    with TickerProviderStateMixin {
  late TabController tabController;

  @override
  void initState() {
    tabController = TabController(length: 3, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var isLightMode = ref.watch(isLightModeProvider);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "首页",
          style: TextStyle(color: Theme.of(context).primaryColor),
        ),
        actions: [
          //主题切换按钮
          IconButton(
            onPressed: () {
              ref.read(isLightModeProvider.notifier).switchMode();
            },
            icon: Icon(isLightMode ? Icons.dark_mode : Icons.light_mode),
          ),
        ],
        //tab栏
        bottom: TabBar(
          controller: tabController,
          tabs: [Tab(text: "推荐"), Tab(text: "动物"), Text("植物")],
        ),
      ),

      //搜索按钮
      floatingActionButton: floatButton(context),

      body: TabBarView(
        controller: tabController,
        children: [
          RecommendPage(),
          AnimalListPage(),
          PlantListPage()
        ],
      ),
      //内容区域
    );
  }
}
